declare var $: any;
$(function () {
    // player 选择的选项
    let playerChecked: string
    gameOptionFn()
    // 游戏选择区域点击事件
    function gameOptionFn(): void {
        $(".hand-box").find("div").click((e: any) => {
            playerChecked = $(e.target).text()
            renderPlayer(playerChecked)
        })
    }
    // 给 player 区域赋值
    function renderPlayer(platerText: string): void {
        $(".player").text(platerText)
        renderAi()
    }
    // AI 区域
    function renderAi(): void {
        let timerNum: number = 4
        let timer: any
        timer = setInterval(() => {
            timerNum = timerNum - 1
            $(".aI").text(timerNum)
            if (timerNum === 1) {
                clearInterval(timer)
                // 为 1 后显示 ai 选择结果
                aiSelect()
            }
        }, 1000)
    }
    // AI 选择逻辑
    function aiSelect(): void {
        /* 
            1.生成 随机数 （1 - 3）
            2.根据索引去匹配 ai 的 石头剪刀布
            3.根据 ai 的石头剪刀布 匹配 ai 的石头剪刀布
        */              
        let random: number = Math.floor(Math.random() * 3) + 1
        let aiChecked:string
        if (random === 3) {
            // 布
            aiChecked = "布"
        } else if (random === 2) {
            // 剪刀
            aiChecked = "剪刀"
        } else { 
            // 石头
            aiChecked = "石头"
        }
        $(".aI").text(aiChecked)
        playerResultAi(aiChecked)
    }
    // player ai result 比较逻辑
    function playerResultAi(aiChecked:String): void {
        let result:unknown
        if (playerChecked === aiChecked) {
            result = "猜拳结果相同"
        } else if (playerChecked === "石头" && aiChecked === "剪刀" || playerChecked === "剪刀" && aiChecked === "布" || playerChecked === "布" && aiChecked === "石头") {
            result = "您胜利了"
        } else { 
            result = "很遗憾，您输了"
        }
        $(".reasult-box").text(result)
    }

})
